package com.gxljc.bear.crawler.proxy;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;

import com.gxljc.bear.crawler.base.DolphinCrawlerConf;
import org.apache.hadoop.conf.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author sugan
 * @since 2014-10-07
 */
public class ProxyManager {
    private final int defaultQueueSize = 200;
    private final LinkedBlockingQueue<IpObject> ipQueue = new LinkedBlockingQueue<IpObject>();
    private gxljcIpProxy gxljcIpProxy = null;
    public static final Logger LOG = LoggerFactory
            .getLogger(ProxyManager.class);

    private ProxyManager() {
    }

    private static class SingletonHolder {
        public static ProxyManager proxyManager = new ProxyManager();
    }

    public static ProxyManager getInstance() {
        return SingletonHolder.proxyManager;
    }

    // 初始化ip服务和列表
    public void init(Configuration conf) throws IpServiceException {
        boolean useProxy = conf.getBoolean("proxy.serv.use", false);
        if (!useProxy) {
            System.out.println("不使用IP代理服务");
            return;
        }

        if (gxljcIpProxy == null) {
            synchronized (ProxyManager.class) {
                if (gxljcIpProxy == null) {
                    gxljcIpProxy = new gxljcIpProxy(conf);
                }
            }
        }

        refreshIpQueue();
    }

    public void initIpQueue() {
        if (ipQueue == null) return;
        ipQueue.clear();
    }

    // 刷新代理IP队列
    private void refreshIpQueue() throws IpServiceException {
        List<IpObject> ipList = gxljcIpProxy.getIps(defaultQueueSize);

        if (ipList != null && !ipList.isEmpty()) {
            for (IpObject ipObject : ipList) {
                if (!ipQueue.contains(ipObject)) {
                    ipQueue.add(ipObject);
                }
            }
        }
    }

    public Proxy getProxy() {
        if (gxljcIpProxy != null) {
            if (ipQueue.isEmpty()) {
                try {
                    refreshIpQueue();
                } catch (IpServiceException e) {
                    LOG.error("获取代理ip列表出错", e);
                }
            }
            if (!ipQueue.isEmpty()) {
                IpObject ip = ipQueue.poll();
                Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(
                        ip.getHost(), ip.getPort()));
                return proxy;
            }
        }
        return null;
    }

    public IpObject getIpObject() {
        if (gxljcIpProxy != null) {
            if (ipQueue.isEmpty()) {
                try {
                    refreshIpQueue();
                } catch (IpServiceException e) {
                    LOG.error("获取代理ip列表出错", e);
                }
            }
            if (!ipQueue.isEmpty()) {
                IpObject ip = ipQueue.poll();
                return ip;
            }
        }
        return null;
    }

    public static void main(String[] args) throws IpServiceException {
        ProxyManager manager = ProxyManager.getInstance();
        manager.init(DolphinCrawlerConf.getInstance());
        Proxy pro = manager.getProxy();
        System.out.println(pro);

    }
}
